﻿using System.Windows.Controls;
using System.Windows.Data;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Controls
{
    /// <summary>
    /// Interaction logic for CeremonyBusinessRulesControl.xaml
    /// </summary>
    public partial class CeremonyPropertiesControl
    {
        private ParvisPropertiesPresentationModel model;

        public CeremonyPropertiesControl()
        {
            InitializeComponent();

            KeyUp += UserControl_KeyUp;
        }

        public ParvisPropertiesPresentationModel Model
        {
            get { return DataContext as ParvisPropertiesPresentationModel; }
            set { DataContext = value; }
        }

        private void UserControl_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            ToggleButtons();
            e.Handled = true;
        }

        private void CancelButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            model = DataContext as ParvisPropertiesPresentationModel;

            if (model != null)
            {
                TextBoxOrdinaryPreliminaryBookingEarliestDays.Text = model.OrdinaryPreliminaryBookingEarliestDays.ToString();
                TextBoxIndividualPreliminaryBookingEarliestDays.Text = model.IndividualPreliminaryBookingEarliestDays.ToString();
                TextBoxOrdinaryImpedimentDocReminderAtDays.Text = model.OrdinaryImpedimentDocReminderAtDays.ToString();
                TextBoxIndividualImpedimentDocReminderAtDays.Text = model.IndividualImpedimentDocReminderAtDays.ToString();
                TextBoxOrdinaryImpedimentDocRemoveBookingAtDays.Text = model.OrdinaryImpedimentDocRemoveBookingAtDays.ToString();
                TextBoxIndividualImpedimentDocRemoveBookingAtDays.Text = model.IndividualImpedimentDocRemoveBookingAtDays.ToString();
                TextBoxReminderOfDocumentsIndividualBookingDays.Text = model.ReminderOfDocumentsIndividualBookingDays.ToString();
                TextBoxReminderOfServiceOrdinaryBookingDays.Text = model.ReminderOfServiceOrdinaryBookingDays.ToString();
                TextBoxImpedimentDocValidMonths.Text = model.ImpedimentDocValidMonths.ToString();
                TextBoxApplicationChangeDateIndividualBookingDays.Text = model.ApplicationChangeDateIndividualBookingDays.ToString();
                TextBoxApplicationChangeDateOrdinaryBookingDays.Text = model.ApplicationChangeDateOrdinaryBookingDays.ToString();
            }

            CancelButton.IsEnabled = false;
            SaveButton.IsEnabled = false;
        }

        private void SaveButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            BindingExpression be = TextBoxOrdinaryPreliminaryBookingEarliestDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxIndividualPreliminaryBookingEarliestDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxOrdinaryImpedimentDocReminderAtDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxIndividualImpedimentDocReminderAtDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxOrdinaryImpedimentDocRemoveBookingAtDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxIndividualImpedimentDocRemoveBookingAtDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxReminderOfDocumentsIndividualBookingDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxReminderOfServiceOrdinaryBookingDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxImpedimentDocValidMonths.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxApplicationChangeDateIndividualBookingDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            be = TextBoxApplicationChangeDateOrdinaryBookingDays.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();

            ToggleButtons();
       }

        private void ToggleButtons()
        {
            model = DataContext as ParvisPropertiesPresentationModel;

            if (model != null)
            {
                bool enabled = false;

                enabled = !TextBoxOrdinaryPreliminaryBookingEarliestDays.Text.Equals(model.OrdinaryPreliminaryBookingEarliestDays.ToString()) || enabled;
                enabled = !TextBoxIndividualPreliminaryBookingEarliestDays.Text.Equals(model.IndividualPreliminaryBookingEarliestDays.ToString()) || enabled;
                enabled = !TextBoxOrdinaryImpedimentDocReminderAtDays.Text.Equals(model.OrdinaryImpedimentDocReminderAtDays.ToString()) || enabled;
                enabled = !TextBoxIndividualImpedimentDocReminderAtDays.Text.Equals(model.IndividualImpedimentDocReminderAtDays.ToString()) || enabled;
                enabled = !TextBoxOrdinaryImpedimentDocRemoveBookingAtDays.Text.Equals(model.OrdinaryImpedimentDocRemoveBookingAtDays.ToString()) || enabled;
                enabled = !TextBoxIndividualImpedimentDocRemoveBookingAtDays.Text.Equals(model.IndividualImpedimentDocRemoveBookingAtDays.ToString()) || enabled;
                enabled = !TextBoxReminderOfDocumentsIndividualBookingDays.Text.Equals(model.ReminderOfDocumentsIndividualBookingDays.ToString()) || enabled;
                enabled = !TextBoxReminderOfServiceOrdinaryBookingDays.Text.Equals(model.ReminderOfServiceOrdinaryBookingDays.ToString()) || enabled;
                enabled = !TextBoxImpedimentDocValidMonths.Text.Equals(model.ImpedimentDocValidMonths.ToString()) || enabled;
                enabled = !TextBoxApplicationChangeDateIndividualBookingDays.Text.Equals(model.ApplicationChangeDateIndividualBookingDays.ToString()) || enabled;
                enabled = !TextBoxApplicationChangeDateOrdinaryBookingDays.Text.Equals(model.ApplicationChangeDateOrdinaryBookingDays.ToString()) || enabled;

                SaveButton.IsEnabled = enabled;
                CancelButton.IsEnabled = enabled;
            }
        }
    }
}
